package com.cskaoyan.service.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.mallbean.KeyWordExample;
import com.cskaoyan.bean.userbean.SearchHistory;
import com.cskaoyan.bean.userbean.SearchHistoryExample;
import com.cskaoyan.bean.wxsearchbean.WxKeyword;
import com.cskaoyan.bean.wxsearchbean.WxKeywordExample;
import com.cskaoyan.mapper.goodsmapper.GoodsMapper;
import com.cskaoyan.mapper.usermapper.SearchHistoryMapper;
import com.cskaoyan.mapper.wxsearchmapper.WxKeywordMapper;
import com.cskaoyan.vo.wx.WxSearchIndexVo;
import com.cskaoyan.vo.wx.WxhistoryKeywordVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import sun.nio.cs.FastCharsetProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: feisl
 * @Date: 2021/5/13 10:08
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    WxKeywordMapper wxKeywordMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public WxSearchIndexVo index() {
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        Integer userId = MyUtils.getUserId();

        //
        WxKeywordExample wxKeywordExample = new WxKeywordExample();
        wxKeywordExample.createCriteria().andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        List<WxKeyword> wxKeywords = wxKeywordMapper.selectByExample(wxKeywordExample);
        WxKeyword defaultKeyword = new WxKeyword();
        for (WxKeyword wxKeyword : wxKeywords) {
            defaultKeyword = wxKeyword;
            break;
        }
        wxSearchIndexVo.setDefaultKeyword(defaultKeyword);
        //


        //
        WxKeywordExample wxKeywordExample1 = new WxKeywordExample();
        ArrayList<WxhistoryKeywordVo> wxhistoryKeywordVoList = new ArrayList<>();
        if (userId != null) {
            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            searchHistoryExample.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
            List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
            for (SearchHistory searchHistory : searchHistories) {
                WxhistoryKeywordVo temp = new WxhistoryKeywordVo();
                temp.setKeyword(searchHistory.getKeyword());
                wxhistoryKeywordVoList.add(temp);
            }
        }
        wxSearchIndexVo.setHistoryKeywordList(wxhistoryKeywordVoList);
        //


        //
        WxKeywordExample wxKeywordExample2 = new WxKeywordExample();
        wxKeywordExample2.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<WxKeyword> hotKeywordList = wxKeywordMapper.selectByExample(wxKeywordExample2);
        wxSearchIndexVo.setHotKeywordList(hotKeywordList);
        //

        return wxSearchIndexVo;
    }

    @Override
    public List<String> helper(String keyword) {

        WxKeywordExample wxKeywordExample = new WxKeywordExample();
        wxKeywordExample.createCriteria().andKeywordLike("%" + keyword + "%").andDeletedEqualTo(false);
        List<WxKeyword> wxKeywords = wxKeywordMapper.selectByExample(wxKeywordExample);
        List<String> res = new ArrayList<>();
        for (WxKeyword wxKeyword : wxKeywords) {
            res.add(wxKeyword.getKeyword());
        }
        return res;
    }

    @Override
    public void clearhistory() {
        Integer userId = MyUtils.getUserId();

        searchHistoryMapper.clean(userId);

    }
}
